#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:log_decorator.py
# author:张世航
# datetime:2024/11/10 17:05
# software: PyCharm
"""
this is function  description 
"""
# import module your need

# 2  编写一个装饰器，能记录其他函数调用的日志，将日志写入到文件中；

import logging
import functools

def log_decorator(filename='function_log.txt'):
    def decorator(func):
        @functools.wraps(func)
        def wrapper(*args, **kwargs):
            logging.basicConfig(filename=filename, level=logging.INFO,
                                format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            logging.info(f"Calling function {func.__name__} with args {args} and kwargs {kwargs}")
            result = func(*args, **kwargs)
            logging.info(f"Function {func.__name__} returned {result}")
            return result
        return wrapper
    return decorator

# 示例
@log_decorator()
def example_function(x, y):
    return x + y

example_function(2, 3)